
find_package(PkgConfig)
pkg_check_modules(PC_EXPAT expat)

find_path(EXPAT_INCLUDE_DIR
    NAMES expat.h
    HINTS ${PC_EXPAT_INCLUDE_DIRS}
    ${PC_EXPAT_INCLUDEDIR}
)

find_library(EXPAT_LIBRARY
    NAMES expat expatd
    HINTS ${PC_EXPAT_LIBDIR}
    ${PC_EXPAT_LIBRARY_DIRS}
)

if(EXPAT_INCLUDE_DIR AND EXPAT_LIBRARY)
    set(EXPAT_FOUND TRUE)
    set(EXPAT_INCLUDE_DIRS ${EXPAT_INCLUDE_DIR})
    set(EXPAT_LIBRARIES ${EXPAT_LIBRARY})
    set(EXPAT_VERSION ${PC_EXPAT_VERSION})
    include(FindPackageHandleStandardArgs)
    FIND_PACKAGE_HANDLE_STANDARD_ARGS(EXPAT  FOUND_VAR EXPAT_FOUND VERSION_VAR EXPAT_VERSION 
	    REQUIRED_VARS EXPAT_LIBRARIES EXPAT_LIBRARY EXPAT_INCLUDE_DIRS EXPAT_INCLUDE_DIR)
    mark_as_advanced(EXPAT_LIBRARIES EXPAT_LIBRARY EXPAT_INCLUDE_DIRS EXPAT_INCLUDE_DIR)

   if(NOT TARGET EXPAT::EXPAT)
      add_library(EXPAT::EXPAT UNKNOWN IMPORTED)
      set_target_properties(EXPAT::EXPAT PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES "C"
        IMPORTED_LOCATION "${EXPAT_LIBRARY}"
        INTERFACE_INCLUDE_DIRECTORIES "${EXPAT_INCLUDE_DIRS}")
    endif()
endif()

